package com.mfoyou.agent.web.controller.main;

import java.net.SocketTimeoutException;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;

import org.apache.http.conn.ConnectTimeoutException;
import org.mfoyou.agent.utils.common.HttpsUtils;
import org.mfoyou.agent.utils.common.JsonRet;
import org.mfoyou.agent.utils.common.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mfoyou.agent.web.intercepter.UrlRight;
import com.mfoyou.agent.web.intercepter.UserRight;
import com.mfoyou.agent.web.service.account.AgentService;
import com.mfoyou.agent.web.service.session.SessionService;
import com.mfoyou.agent.web.service.thirdPart.AmapService;
import com.mfoyou.agent.web.service.wx.WXBaseService;
import com.mfoyou.agent.web.utils.network.WebUtils;
import com.mfoyou.agent.web.utils.redis.RedisCacheManger;

/**
 * MainController
 *
 * @author wb-zh270168
 */
@Path("/")
@Service
public class MainContriller {

    @Autowired
    SessionService sessionService;
    @Autowired
    WXBaseService wXBaseService;
    @Autowired
    AmapService amapService;
    @Autowired
    AgentService agentService;
    @Autowired
    RedisCacheManger redisCacheManger;

    @GET
    @Path(value = "")
    @Produces("text/html;charset=UTF-8")
    @UrlRight(UserRight.ALLACCESS)
    public String getFileInfo(@Context HttpServletRequest request) {
        return "<div style=' text-align:center;'>Welcome to mfoyou</div><br/><div style=' text-align:center;'> you ip is :" + WebUtils.getIpAddr(request) + "</div>";
    }

    @GET
    @Path(value = "token")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.ALLACCESS)
    public JsonRet<?> getToken(@Context HttpServletRequest request) {
        String token = SessionService.getSeesionId(request);
        return JsonRet.buildSuccRet(token);
    }

    @GET
    @Path(value = "test/token")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.ALLACCESS)
    public JsonRet<?> testToken(@Context HttpServletRequest request, @QueryParam("lon") Double lon, @QueryParam("lat") Double lat) {
        Integer userId = sessionService.currentUserId(request);
        if (userId == null) {
            return JsonRet.buildSuccRet(-1);
        } else {
            return JsonRet.buildSuccRet(0);
        }
    }

    @GET
    @Path(value = "test/info/{id}")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.ALLACCESS)
    public JsonRet<?> testToken(@Context HttpServletRequest request, @PathParam("id") Integer id) {
        return JsonRet.buildSuccRet(null);
    }

    @GET
    @Path(value = "test/gdd1")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.ALLACCESS)
    public JsonRet<?> testhmd(@Context HttpServletRequest request) throws ConnectTimeoutException, SocketTimeoutException, Exception {

        return JsonRet.buildSuccRet(
                HttpsUtils.post("https://datasub.bbdservice.com/api/bbd_sub/uploadJSON", "[{\"companyName\":\"万科企业股份有限公司\"},{\"companyName\":\"中国农业银行股份有限公司\"}]", "application/json", "utf-8", 100, 100));
    }

}
